﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCE.UI
{
    public partial class ZMCEB0 : EFViewCmes
    {
        private EFTreeItem item;    //记录选中的树节点
        public ZMCEB0()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ZMCEA0_Loaded);
        }
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ZMCEA0_Loaded(object sender, RoutedEventArgs e)
        {
            this.tree.Refresh();

        }
        /// <summary>
        /// 选中树节点，导航右侧的页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tree_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            item = this.tree.SelectedItems[0] as EFTreeItem;

            Dictionary<string, string> args = new Dictionary<string, string>();
            string _value = item.ItemObject.GetType().GetProperty("label").GetValue(item.ItemObject, null) as string;
            string leaf = (item.IsLeaf == true) ? "1" : "0";

            if (leaf.Equals("0"))
            {
                string toencode = item.ItemObject.GetType().GetProperty("toencode").GetValue(item.ItemObject, null) as string;
                args.Add("fdToencode", toencode);
                this.container.LoadView("ZMCEB1", args);
            }
            else
            {
                args.Add("fdKeyId", _value);
                this.container.LoadView("ZMCEB6", args);
            }
        }

        /// <summary>
        /// 展开树节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tree_ItemPrepared(object sender, Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e)
        {
            if (e.PreparedItem == null)
            {
                return;
            }
            EFTreeItem treeItem = e.PreparedItem as EFTreeItem;
            if (treeItem.Level == 0)
            {
                this.tree.SelectedItem = treeItem;
                treeItem.ExpandItem();//展开
            }            
        }


        // /// <summary>
        // /// 重新基类的发布数据方法
        // /// 刷新树节点
        // /// </summary>
        // /// <param name="data"></param>
        public override void OnEIDataPublished(Platform.Core.EIInfo data)
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            if (item != null)
            {
                string classLeaf = item.ItemObject.GetType().GetProperty("classLeaf").GetValue(item.ItemObject, null) as string;
                //if ("1".Equals(classLeaf))
                //{
                    args.Add("classLeaf", classLeaf);
                    string _value = item.ItemObject.GetType().GetProperty("label").GetValue(item.ItemObject, null) as string;
                    args.Add("node", _value);
                    BaseTools.RefreshTree("ZMCEB0", "query", args, OnDoWorkCompleted);
                //}
            }
        }

        /// <summary>
        /// OnEIDataPublished回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            this.tree.EIDataContext = new Platform.Core.UI.EIDataContext();
            this.tree.EIDataContext.Update(e.EIInfo);
            item.IsLoadOnDemandEnabled = true;
            item.Items.Clear();
            item.CollapseItem();
            item.ExpandItem();
        }

    }
}
